#!/bin/sh

cd `pwd`

STR=`ps -ef | grep "com.hz.api.admin.web.ApiAdminApplication" | awk  -F ' '  '{print $2}'`
if [ ! -z "$STR" ]; then
    kill -9 $STR > /dev/null 2>&1
    sleep 4
else
	echo "没有Java进程"
fi

APP_HOME=`readlink -f $0`
APP_HOME=`dirname $APP_HOME`
APP_HOME=`dirname $APP_HOME`

DIR_LOG=${APP_HOME}/../logs/admin

if [ ! -e $DIR_LOG ] ; then
    mkdir -p $DIR_LOG
fi

FILE_STDOUT_LOG=$DIR_LOG/stdout.log
FILE_STDERR_LOG=$DIR_LOG/stderr.log


JAVA_HOME="";
echo "APP_HOME: ${APP_HOME}"

if [ ! -e ${APP_HOME}/jre ]; then
    if [ ! -e ${APP_HOME}/jre.tar.gz ]; then
        echo "WARN: can not find the jre.tar.gz file under ${APP_HOME}"
        echo "WARN: start find the jre.zip file under ${APP_HOME}"

        if [ ! -e ${APP_HOME}/jre.zip ]; then
            echo "ERROR: can not find the jre.zip file under ${APP_HOME}"
            exit 1
        else
            unzip ${APP_HOME}/jre.zip -d ${APP_HOME}/ > /dev/null
            rm ${APP_HOME}/jre.zip
        fi
    else
        tar -xvf ${APP_HOME}/jre.tar.gz -C ${APP_HOME} > /dev/null
        rm ${APP_HOME}/jre.tar.gz
    fi
fi


if [ ! -e ${APP_HOME}/jre ]; then
    echo "ERROR: can not find jre under ${APP_HOME}..."
    exit 1
fi

JAVA_HOME=${APP_HOME}/jre
export JAVA_HOME
export APP_HOME
export LANG=zh_CN.UTF-8
export TZ=Asia/Shanghai


OS_NAME=`uname -s`
# compare version1 adn version2, return true if version1 great than version2
function version_gt() {
        if [ "${OS_NAME}" == "Linux" ]; then
                test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1";
        else
                test "$(echo "$@" | tr " " "\n" | sort -c | head -n 1)" != "$1";
        fi
}



#DEBUG_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=6888,server=y,suspend=n"

JAVA_OPT="-Xmx1344M -Xms1344M -Xmn512M -XX:MaxMetaspaceSize=256M -XX:MetaspaceSize=256M -XX:+UseConcMarkSweepGC -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses -XX:+CMSClassUnloadingEnabled -XX:+ParallelRefProcEnabled -XX:+CMSScavengeBeforeRemark -XX:ErrorFile=${DIR_LOG}/hs_err_pid%p.log  -Xloggc:${DIR_LOG}/gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${DIR_LOG}/dump -XX:+PrintGCDetails -XX:+PrintGCDateStamps";
${JAVA_HOME}/bin/java $DEBUG_OPTS -cp "${APP_HOME}/lib/*"  ${JAVA_OPT} -Dfile.encoding=UTF-8 -Dlog.logDir=${DIR_LOG}  -Dspring.profiles.active=mysql com.hz.api.admin.web.ApiAdminApplication > ${FILE_STDOUT_LOG} 2>${FILE_STDERR_LOG} &
